package com.xd.pre.modules.tpshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.pre.common.utils.Result;
import com.xd.pre.modules.tpshop.domain.*;
import com.xd.pre.modules.tpshop.service.*;
import com.xd.pre.modules.tpshop.util.CommonFunction;
import com.xd.pre.modules.tpshop.virtuaBean.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/** 不要格式化
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/goods")
public class TpGoodsController {


    @Autowired
    private ITpGoodsService goods_biz;
    @Autowired
    private ITpSpecGoodsPriceService ITpSpecGoodsPriceBiz_biz;
    @Autowired
    private ITpGoodsAttrService attr_biz;
    @Autowired
    private ITpBrandService brand_biz;
    @Autowired
    private ITpSpecItemService TpSpecItem_biz;

//    @Autowired
//    private ITpGoodsAttributeAction attr_acation;
    @Autowired
    private ITpGoodsAttributeService attribute_biz;
//    @Autowired
//    private ITpGoodsAttributeService ga_biz;

//    @Autowired
//    private ITpSpecGoodsPriceAction spec_action;
    @Autowired
    private ITpSpecService TpSpec_biz;





    //【1】 http://localhost:8080/tpshop_server/goods/findgoodslist_byCategory2?categoryId=191
    //	categoryId=100: "电池 电源 充电器" ---> 这个时候的抽屉内容比较多，便于测试！//
    @RequestMapping(value="/findgoodslist_byCategory2",method=RequestMethod.GET)

    public Result findgoodslist_byCategory2(HttpServletRequest request){ //
        Result result = new Result();
        System.out.println("测试--1-request.getParameter(categoryId)-->: "+request.getParameter("categoryId")); //

        Map<String, Map<String, Object>> mm2 = new HashMap<String, Map<String, Object>>();
        Map<String, Object> mm1 = new HashMap<String, Object>();
        List<TpGoods> theGoodsList = new ArrayList<TpGoods>();
        String the_categoryId = request.getParameter("categoryId"); //
        if(null != the_categoryId && !"".equals(the_categoryId)) {
            List<TpGoods> goodsList =  goods_biz.findgoodslist_byCategory(Integer.parseInt(the_categoryId));

            //以下两种封装json的方法均不推荐！！！
//			for (int i = 0; i < goodsList.size(); i++) {
//
//
////				mm1.put("goods_id", goodsList.get(i).getGoodsId());
////				mm1.put("goods_name", goodsList.get(i).getGoodsName());
////				mm1.put("original_img", goodsList.get(i).getOriginalImg());
////				mm1.put("category_id", goodsList.get(i).getCategoryId());
////
////				mm2.put(""+i, mm1);
//
////				TpGoods goodsItem= new TpGoods(
////							goodsList.get(i).getGoodsId(),
////							goodsList.get(i).getGoodsName(),
////							goodsList.get(i).getOriginalImg(),
////							goodsList.get(i).getCategoryId()
////				);
////				theGoodsList.add(goodsItem);
//
//			}
            result.setData(goodsList);
        }

        return result;
    }

    //【3】 http://localhost:8080/tpshop_server/goods/findgoodslist_byCategory3 			//
    @RequestMapping(value="/findgoodslist_byCategory3",method=RequestMethod.GET)

    public Result findgoodslist_byCategory3(HttpServletRequest request){ //
        Result result = new Result();

        Map<Object, Map<Object, Object>> mm2 = new HashMap<Object, Map<Object, Object>>();
        Map<Object, Object> mm1 = new HashMap<Object, Object>();
        List<TpGoods> theGoodsList = new ArrayList<TpGoods>();
        List<Integer> categoryIdList = goods_biz.list().stream().map(TpGoods::getCategoryId).collect(Collectors.toList());
//        List<Integer> categoryIdList = goods_biz.findCategoryIdList_bygoodslistALL(); // find 表中所有的 categoryId集合
        Set<Integer> set = new HashSet<Integer>();
        if(null != categoryIdList && !"".equals(categoryIdList)) {
            for (int i = 0; i < categoryIdList.size(); i++) {
                set.add(categoryIdList.get(i)); //set 去重
            }

            Map<String, List<TpGoods>> mm5 = new HashMap<String, List<TpGoods>>();

            List<TpGoods> goodsList = null;
            Iterator <Integer> itt = set.iterator();
            while(itt.hasNext()){
                System.out.println("itt.next(): "+ itt.next());
//				mm5.put("category_id", itt.next());

                mm5.put("goodslist_by_category_id", goods_biz.findgoodslist_byCategory(itt.next()) );

//				goodsList = new ArrayList<TpGoods>();
//				goodsList.add((TpGoods) goods_biz.findgoodslist_byCategory(itt.next()) );
            }



            result.setData(mm5);
        }

        return result;
    }


//	//【4】 http://localhost:8080/tpshop_server/goods/findGoods_bygoodsId?goodsId=1 			// // 无： List<SPProductAttribute> ;  List<SPProductSpec>
//	@RequestMapping(value="/findGoods_bygoodsId",method=RequestMethod.GET)
//
//	public Result findGoods_bygoodsId(HttpServletRequest request){
//		Result result = new Result();
//
//		System.out.println("测试--1-request.getParameter(goodsId)-findGoods_bygoodsId->: "+request.getParameter("goodsId")); //
//		String goodsId = request.getParameter("goodsId"); //
//
//		TpGoods thegoods = null;
//		if(null != goodsId && !"".equals(goodsId)){
//			thegoods = goods_biz.findGoods_bygoodsId(Integer.parseInt(goodsId));
//
//			result.setData(thegoods);
//		}
//
//		return result;
//	}

    //【5】 http://localhost:8080/tpshop_server/goods/findGoods_bygoodsId?goodsId=1 			// 无： List<SPProductAttribute> ;  List<SPProductSpec>
    @RequestMapping(value="/findGoods_bygoodsId",method=RequestMethod.GET)

    public Result findGoods_bygoodsId(HttpServletRequest request){
        Result result = new Result();

        System.out.println("测试--1-request.getParameter(goodsId)-findGoods_bygoodsId->: "+request.getParameter("goodsId")); //
        String goodsId = request.getParameter("goodsId"); //

        if(null != goodsId && !"".equals(goodsId)){
            JSONObject jb1 = new JSONObject();
            TpGoods thegoods = findTpGoodsById(goodsId);
//            TpGoods thegoods = goods_biz.findGoods_bygoodsId(Integer.parseInt(goodsId));
            jb1.put("goods", thegoods);
            //		jb2.put("goods", jb1);

            Map<String, SpecGoodsPrice_virtual> map =  SpecGoodsPrice2Virtual( goodsId);
            jb1.put("spec_goods_price", map);

            result.setData(jb1);
        }

        return result;
    }

    private TpGoods findTpGoodsById(String goodsId) {
        return goods_biz.getById(goodsId);
    }

    public Map<String, SpecGoodsPrice_virtual> SpecGoodsPrice2Virtual(String goodsId){ //通用方法
        if(CommonFunction.stringCheckNotNull(goodsId)){
            List<TpSpecGoodsPrice> vir_list = getTpSpecGoodsPricesByGoodsId(goodsId);
//            List<TpSpecGoodsPrice> vir_list = ITpSpecGoodsPriceBiz_biz.find_SpecGoodsPrice_byGoodsId(Integer.parseInt(goodsId));
            Map<String, SpecGoodsPrice_virtual> map = new HashMap<String, SpecGoodsPrice_virtual>();
            SpecGoodsPrice_virtual sgp_vir = null;
            if(vir_list!=null && !vir_list.isEmpty()){
                for (int i = 0; i < vir_list.size(); i++) {
                    sgp_vir = new SpecGoodsPrice_virtual(
                            vir_list.get(i).getKey(),
                            vir_list.get(i).getPrice().doubleValue(),
                            vir_list.get(i).getStoreCount()

                    );
                    map.put(vir_list.get(i).getKey(), sgp_vir);
                }
            }
            return map;
        }
        return null;
    }

    private List<TpSpecGoodsPrice> getTpSpecGoodsPricesByGoodsId(String goodsId) {
        LambdaQueryWrapper<TpSpecGoodsPrice> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpSpecGoodsPrice::getGoodsId, goodsId);
        return ITpSpecGoodsPriceBiz_biz.list(lambdaQuery);
    }


    //【6】 http://localhost:8080/tpshop_server/goods/findGoods_3list_bygoodsId?goodsId=1 // 含有： List<SPProductAttribute> ;  List<SPProductSpec> 验证成功
    //其他用途：京东上“确认订单”界面 intent到“商品清单”界面，在“商品清单”界面 上是不允许 再次点击跳转到 商品详情界面的！ 如果想要可以跳转的效果的话，app可利用当前方法 获取界面data
    @RequestMapping(value="/findGoods_3list_bygoodsId",method=RequestMethod.GET)

    public Result findGoods_3list_bygoodsId(HttpServletRequest request){
        Result result = new Result();

        System.out.println("测试--1-request.getParameter(goodsId)-findGoods_bygoodsId->: "+request.getParameter("goodsId")); //
        String goodsId = request.getParameter("goodsId"); //
        if(CommonFunction.stringCheckNotNull(goodsId)){ // must

            JSONObject jb1 = new JSONObject();
            TpGoods thegoods = findTpGoodsById(goodsId);
//            TpGoods thegoods = goods_biz.findGoods_bygoodsId(Integer.parseInt(goodsId));
            jb1.put("goods", thegoods);

            List<Attribute_virtual> av_list = attribute_biz.Attribute2Virtual(goodsId);
            List<GoodsSpec_virtual> gsv_list = TpSpec_biz.GoodsSpec2Virtual(goodsId);
            Map<String, SpecGoodsPrice_virtual> map =  SpecGoodsPrice2Virtual( goodsId);

            jb1.put("goods_attr_list", av_list);
            jb1.put("goods_spec_list", gsv_list);
            jb1.put("spec_goods_price", map);

            result.setData(jb1);
        }

        return result;
    }





//	//【10】 http://localhost:8080/tpshop_server/goods/findPic_bygoodsId?goodsId=1 			//
//	@RequestMapping(value="/findPic_bygoodsId",method=RequestMethod.GET)
//
//	public String findPic_bygoodsId(HttpServletRequest request){
//		Result result = new Result();
//
//		System.out.println("测试--1-request.getParameter(goodsId)-findGoods_bygoodsId->: "+request.getParameter("goodsId")); //
//		String goodsId = request.getParameter("goodsId"); //
//
//		int width  = 100;
//		int height = 100;
//
//		//判断缩略图是否存在
//	   String path = "Public/upload/goods/thumb/$goods_id/";
//	   String goods_thumb_name ="goods_thumb" + "_" + goodsId + "_" + width + "_" + height;
//
//	   // 这个商品 已经生成过这个比例的图片就直接返回了
//	   if( new File("c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".jpg").exists() ) return "/"+ "c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".jpg";
//	   if( new File("c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".jpeg").exists() ) return "/"+ "c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".jpeg";
//	   if( new File("c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".gif").exists() ) return "/"+ "c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".gif";
//	   if( new File("c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".png").exists() ) return "/"+ "c:"+ File.separator + "tpShopDiy_Img" + path + goods_thumb_name + ".png";
//
//
//	   String goods_Original_Img = null; //thegoods.getOriginalImg(); // /Public/upload/goods/2016/03-09/56e01a4088d3b.jpg
//		File file=new File("c:"+ File.separator + "tpShopDiy_Img" + goods_Original_Img );
//	   try {
//
//       Thumbnails.of(file)
//			       .size(width, height)
//			       .toFile("c:"+ File.separator + "tpShopDiy_Img" + path); ;
//       } catch(Exception e) {
//           e.printStackTrace() ;
//       }
//
//
//		TpGoods thegoods = null;
//		if(null != goodsId && !"".equals(goodsId)){
//			thegoods = goods_biz.findGoods_bygoodsId(Integer.parseInt(goodsId));
//			if(null != thegoods && !"".equals(thegoods)){
//				String goods_Original_Img = thegoods.getOriginalImg(); // /Public/upload/goods/2016/03-09/56e01a4088d3b.jpg
//				File file=new File("c:"+ File.separator + "tpShopDiy_Img" + goods_Original_Img );
//				try {
//					InputStream is=new FileInputStream(file);
//					BufferedInputStream bis=new BufferedInputStream(is);
//					int temp=0;
//					while((temp=bis.read())!=-1){
//						out.write(temp);
//					}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			}
//
//			result.setData(thegoods);
//		}
//
//		return result;
//	}
//








    //id=191 电脑---平板电脑
    //【5】 http://localhost:8080/tpshop_server/goods/findGoodsList_byCategory_filter?id=191 			// 无： List<SPProductAttribute> ;  List<SPProductSpec>
    @RequestMapping(value="/findGoodsList_byCategory_filter",method=RequestMethod.GET)

    public Result findGoodsList_byCategory_filter(HttpServletRequest request){
        Result result = new Result();
        JSONObject jb_all = new JSONObject();

        System.out.println("测试-1-id->: "+request.getParameter("id")); //  productCondition.categoryID
        System.out.println("测试-2-p->: "+request.getParameter("p")); //
        System.out.println("测试-3-pagesize->: "+request.getParameter("pagesize")); //

        System.out.println("测试-4-orderby->: "+request.getParameter("orderby")); //		可选
        System.out.println("测试-5-orderdesc->: "+request.getParameter("orderdesc")); // 可选

        System.out.println("测试-6-brand_id->: "+request.getParameter("brand_id")); //
        System.out.println("测试-7-price->: "+request.getParameter("price")); //

        System.out.println("测试-8-attr->: "+request.getParameter("attr")); //
        System.out.println("测试-9-spec->: "+request.getParameter("spec")); //

        String categoryId = request.getParameter("id");
        String pageIndex = request.getParameter("p");
        String pageSize = request.getParameter("pagesize");

        String brand_id = request.getParameter("brand_id");
        String price = request.getParameter("price");
        Map<String,String> mm = new HashMap<String,String>(); // 品牌、价格这两个是最基本的，是必选的
        if(CommonFunction.stringCheckNotNull(brand_id) ) {
            mm.put("brand_id", brand_id);
        }
        if(CommonFunction.stringCheckNotNull(price) ) 	{
            mm.put("price", price);
        }

        String attr = request.getParameter("attr");		//"href":"&attr=223_Android "  日后升级:	"&attr=223_Android,223_Android,223_Android... "
        String spec = request.getParameter("spec");		//

        //进入商品list界面 的前一刻 你点击的那个category的id 所拥有的goodsId的list
        //"select goodsId FROM TpGoods WHERE 1=1 and isOnSale=1 and categoryId=?" //diy的时候 categoryId不是一个集合，而是单个的
//        List<Integer> goodsId_list = goods_biz.findGoodsIdList_byCategoryId(Integer.parseInt(categoryId));//filter_goodsId_list
        LambdaQueryWrapper<TpGoods> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpGoods::getIsOnSale, 1);
        lambdaQuery.eq(TpGoods::getCategoryId, categoryId);
        List<TpGoods> list1 = goods_biz.list(lambdaQuery);
        List<Integer> goodsId_list = list1.stream().map(TpGoods::getGoodsId).collect(Collectors.toList());

        System.out.println("测试-20-goodsId_list.size() : "+ goodsId_list.size() );


        if( mm.size() > 0){ //mm.size()>0 才 取交集
            LambdaQueryWrapper<TpGoods> lambda = Wrappers.lambdaQuery();
            if(CommonFunction.stringCheckNotNull(brand_id) ) {
                lambda.eq(TpGoods::getBrandId,brand_id);
            }
            if(CommonFunction.stringCheckNotNull(price) ) 	{
                lambda.eq(TpGoods::getShopPrice,price);
            }
            List<TpGoods> list2 = goods_biz.list(lambda);
            List<Integer> goodsId_list_filter_brandOrPrice = list2.stream().map(TpGoods::getGoodsId).collect(Collectors.toList());
//            List<Integer> goodsId_list_filter_brandOrPrice = goods_biz.findGoodsId_byBrandId( mm );

            goodsId_list.retainAll(goodsId_list_filter_brandOrPrice); // 取交集
        }
        if(CommonFunction.stringCheckNotNull(attr) ) {
            String[] str = attr.split("_");
            //select goodsId FROM TpGoodsAttr WHERE attrId2=?		and attrValue =?  GROUP BY goodsId
//            List<Integer> goodsIdList_filter_attr = attr_biz.findGoodsIdList_byConditions(Integer.parseInt(str[0]), str[1]);

            LambdaQueryWrapper<TpGoodsAttr> lambda2 = Wrappers.lambdaQuery();
            lambda2.eq(TpGoodsAttr::getAttrId2,Integer.parseInt(str[0]));
            lambda2.eq(TpGoodsAttr::getAttrValue,str[1]);
            List<TpGoodsAttr> list3 = attr_biz.list(lambda2);
            List<Integer> goodsIdList_filter_attr = list3.stream().map(TpGoodsAttr::getGoodsId).collect(Collectors.toList());

            goodsId_list.retainAll(goodsIdList_filter_attr); // 取交集
        }

        if(CommonFunction.stringCheckNotNull(spec) ) {
            String[] str = spec.split("_");
            List<Integer> goodsIdList_filter_spec = ITpSpecGoodsPriceBiz_biz.find_goodsId_by_keyLike(str[1]);

            LambdaQueryWrapper<TpGoodsAttr> lambda2 = Wrappers.lambdaQuery();
            lambda2.eq(TpGoodsAttr::getAttrId2,Integer.parseInt(str[0]));
            lambda2.eq(TpGoodsAttr::getAttrValue,str[1]);
            List<TpGoodsAttr> list3 = attr_biz.list(lambda2);
            List<Integer> goodsIdList_filter_attr = list3.stream().map(TpGoodsAttr::getGoodsId).collect(Collectors.toList());

            goodsId_list.retainAll(goodsIdList_filter_spec); // 取交集
        }




        if(CommonFunction.listCheckNotNull(goodsId_list)){	//所以app的逻辑可看出，它是此种模式：没点一下 就会请求一下server进而刷新app界面的数据
            List<CategoryCommonVirtual> bv_list = getBrandVirtualList( goodsId_list);
            jb_all.put("filter_brand", bv_list);													//1. 品牌

            List<Attribute_virtual_forCategory> avf_list_attr = getAttrList( goodsId_list);			//2.atrr
            jb_all.put("filter_attr", avf_list_attr);

            List<Attribute_virtual_forCategory> avf_list_spec = getSpecList(goodsId_list);
            jb_all.put("filter_spec", avf_list_spec);

            result.setData(jb_all);
            return result;
        }




        return result;
    }


    //"5_11":  `tp_spec`（5）		+  `tp_spec_item`（11）
    public List<Attribute_virtual_forCategory> getSpecList(List<Integer> goodsId_list){
        List<String> speList = new ArrayList<String>();
        for (int i = 0; i < goodsId_list.size(); i++) {
            Integer goodsIdItem = goodsId_list.get(i);
            List<TpSpecGoodsPrice> vir_list = getTpSpecGoodsPricesByGoodsId(goodsIdItem+"");
            Set<String> speKey_sppend = vir_list.stream().map(TpSpecGoodsPrice::getKey).collect(Collectors.toSet());
//            String speKey_sppend = ITpSpecGoodsPriceBiz_biz.findSpec_byGoodsId(goodsIdItem);
//            speList.add(speKey_sppend);
            speList.addAll(speKey_sppend);
        }

        Set<String> speSet = new HashSet<String>();
        for (int i = 0; i < speList.size(); i++) {
            String[] str = speList.get(i).split("_");
            for (int j = 0; j < str.length; j++) {
                speSet.add(str[j]);
            }
        }

        List<String> spe_list = new ArrayList<String>(speSet); // set 转 list
        Set<Integer> spec_id_set = new HashSet<Integer>();		//`tp_spec`表主键 set
        for (int i = 0; i < spe_list.size(); i++) {
            String keyId = spe_list.get(i);
            TpSpecItem specItem = TpSpecItem_biz.getById(keyId);
            Integer specId = specItem.getSpecId();
            spec_id_set.add(specId);
//            List<Integer> spec_id_list = TpSpecItem_biz.find_spec_id_by_keyId(Integer.parseInt(keyId));//`tp_spec`表主键 在`tp_spec_item`表中做外键
//            for (int j = 0; j < spec_id_list.size(); j++) {
//                spec_id_set.add(spec_id_list.get(j));
//            }
        }

        List<Integer> spec_id_list = new ArrayList<Integer>(spec_id_set); // set 转 list	//`tp_spec`表主键 list
        List<Attribute_virtual_forCategory> avf_list = new ArrayList<Attribute_virtual_forCategory>();
        for (int i = 0; i < spec_id_list.size(); i++) {
            Integer specIdItem = spec_id_list.get(i);
            List<CategoryCommonVirtual> ccv_list = new ArrayList<CategoryCommonVirtual>();

            LambdaQueryWrapper<TpSpecItem> lambda1 = Wrappers.lambdaQuery();
            lambda1.eq(TpSpecItem::getSpecId, specIdItem);
            List<TpSpecItem> TpSpecItem_list = TpSpecItem_biz.list(lambda1);
//            List<TpSpecItem> TpSpecItem_list = TpSpecItem_biz.find_TpSpecItem_by_spec_id(specIdItem);
            for (int j = 0; j < TpSpecItem_list.size(); j++) {
                CategoryCommonVirtual ccv = new CategoryCommonVirtual(
                        j+"",													//"id"
                        TpSpecItem_list.get(j).getItem(),						//	"name": "4G",
                        "&spec="+ specIdItem + "_" + TpSpecItem_list.get(j).getSpecItemKeyId()	// href append
                        // 							   "5_11":  `tp_spec`（5）		+  `tp_spec_item`（11）
//`tp_spec`表主键 在`tp_spec_item`表中做外键	 		  + "_" +	当前spec_id所对应的`tp_spec_item`表集合的item
                );
                ccv_list.add(ccv);
            }

            Attribute_virtual_forCategory avf = new Attribute_virtual_forCategory(
                    TpSpec_biz.getById(specIdItem).getName(),                   //"name": "网络",
//                  TpSpec_biz.findPo_byKeyId(specIdItem).getName(),			//"name": "网络",
                    ccv_list													//"item":
            );
            avf_list.add(avf);
        }

        return avf_list;
    }


    public List<Attribute_virtual_forCategory> getAttrList(List<Integer> goodsId_list){
        Set<Integer> attrIdSet = new HashSet<Integer>();
        for (int i = 0; i < goodsId_list.size(); i++) {
            List<TpGoodsAttr> attr_List = attribute_biz.find_SPProductAttribute_list_byGoodsId_part1(goodsId_list.get(i));
            if(CommonFunction.listCheckNotNull(attr_List)){
                for(TpGoodsAttr tga : attr_List){
                    attrIdSet.add(tga.getGoodsAttrId());
                }
            }
        }
        List<Integer> attrId_list = new ArrayList<Integer>(attrIdSet); // set 转 list

        Set<Integer> attrId2_set = new HashSet<Integer>();
        for (int i = 0; i < attrId_list.size(); i++) {
            List<TpGoodsAttr> goodsAttrList = attribute_biz.find_SPProductAttribute_value_list_byAttrId_part1(attrId_list.get(i));
            Set<Integer> attrId3 = goodsAttrList.stream().map(TpGoodsAttr::getAttrId2).collect(Collectors.toSet());
//            int attrId2 = goodsAttrList.getAttrId2();
//            attrId2_set.add(attrId2);
            attrId2_set.addAll(attrId3);

        }
        List<Integer> attrId2_list = new ArrayList<Integer>(attrId2_set); // set 转 list

        List<Attribute_virtual_forCategory> avf_list = new ArrayList<Attribute_virtual_forCategory>();
        for (int i = 0; i < attrId2_list.size(); i++) {

            List<CategoryCommonVirtual> ccv_list = new ArrayList<CategoryCommonVirtual>();
            List<TpGoodsAttr> attrList_by_attrId2 = attribute_biz.find_SPProductAttr_list_by_attr_id2(attrId2_list.get(i));

            Set<String> attrValue_set = new HashSet<String>();
            for (int j = 0; j < attrList_by_attrId2.size(); j++) {
                attrValue_set.add(attrList_by_attrId2.get(j).getAttrValue());

//
//				CategoryCommonVirtual ccv = new CategoryCommonVirtual(
//					attrList_by_attrId2.get(j).getGoodsAttrId()+"",			//"id"
//					attrList_by_attrId2.get(j).getAttrValue(),				//"name"
//					"&attr="+ attrList_by_attrId2.get(j).getAttrId2()+"_"+attrList_by_attrId2.get(j).getAttrValue()	// href append
//					//										69		   _
//					);
//				ccv_list.add(ccv);
            }

            List<String> attrValue_list = new ArrayList<String>(attrValue_set); // set 转 list
            for (int k = 0; k < attrValue_list.size(); k++) {
                CategoryCommonVirtual ccv = new CategoryCommonVirtual(
                        k+"",													//"id"
                        attrValue_list.get(k),									//"name"
                        "&attr="+ attrId2_list.get(i)+"_"+attrValue_list.get(k)	// href append
                        //										69		   _
                );
                ccv_list.add(ccv);
            }


            Attribute_virtual_forCategory avf = new Attribute_virtual_forCategory(
                    attribute_biz.getById(attrId2_list.get(i)).getAttrName(),
//                    attribute_biz.findPo_byKeyId(attrId2_list.get(i)).getAttrName(),
                    ccv_list
            );

            avf_list.add(avf);

        }

        return avf_list;
    }

    public List<CategoryCommonVirtual> getBrandVirtualList(List<Integer> goodsId_list){
        Set<Integer> brandId_set = new HashSet<Integer>();
        for (int i = 0; i < goodsId_list.size(); i++) {
            Integer goodsIdTemp = goodsId_list.get(i);
            //brandId_set = new HashSet<Integer>(); 注意：这个new 千万不要写在这里！写在这里的话无法实现 累加 功能！
            TpGoods goods1 = goods_biz.getById(goodsIdTemp);
            Integer brandId_item = goods1.getBrandId();
//            int brandId_item =  goods_biz.findBrandId_byGoodsId(goodsIdTemp);
            brandId_set.add(brandId_item);
        }

        List<Integer> brandId_list = new ArrayList<Integer>(brandId_set); // set 转 list
        List<TpBrand> brand_list = new ArrayList<TpBrand>();
        for (int i = 0; i < brandId_list.size(); i++) {
            Integer brandIdTemp = brandId_list.get(i);
            TpBrand brand = brand_biz.getById(brandIdTemp);
//            TpBrand brand = brand_biz.findBrand_byKeyId(brandIdTemp);
            if(CommonFunction.poCheckNotNull(brand)){
                //brand_list = new ArrayList<TpBrand>();注意：这个new 千万不要写在这里！写在这里的话无法实现 累加 功能！
                brand_list.add(brand);
            }
        }

        List<CategoryCommonVirtual> bv_list = new ArrayList<CategoryCommonVirtual>();
        if(CommonFunction.listCheckNotNull(brand_list)){
            for (int i = 0; i < brand_list.size(); i++) {
                CategoryCommonVirtual bv = new CategoryCommonVirtual(
                        brand_list.get(i).getId()+"",
                        brand_list.get(i).getName(),
                        "&brand_id="+ brand_list.get(i).getId()
//							"http://localhost:8080/tpshop_server/goods/findGoodsList_byCategory_filter?id="+ categoryId +"&brand_id="+ brand_list.get(i).getId()
                        //http://localhost:8080/tpshop_server/goods/findGoodsList_byCategory_filter?id=    191		  &brand_id=
                );
                bv_list.add(bv);
            }
        }
        return bv_list;
/*{"status":"success","tipCode":"","tipMsg":"","data":
   {"filter_brand":[{"id":"1", "name":"华为/HUAWEI","href":"&brand_id=1"},
  					{"id":"15","name":"三星/SAMSUNG","href":"&brand_id=15"}]}}
  	为什么对不上 开发手册：获取所有商品：	http://www.tpshop.com/index.php?m=Api&c=Goods&a=goodsList ： 因为diy了‘goods’表中的insert的数据！
  	另外注意 开发手册中的"id": 75，"id": 76 这个是自定义的吗？？？---> 需要分析app逻辑 来确认！！！(经过app逻辑Monitor,发现这个"id" 并没有用到(应该没什么意思))
*/
    }












/*									"操作系统" 的  id = 69			my: 223
{
                            "name": "操作系统",
                            "item": [
                                     {
                                         "name": "Android 4.4+EMUI 3.0",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_Android 4.4+EMUI 3.0",
                                         "id": 25
                                     },
                                     {
                                         "name": "EMUI 3.1（兼容Android 5.1）",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_EMUI 3.1（兼容Android 5.1）",
                                         "id": 26
                                     },
                                     {
                                         "name": "华为 EMUI 3.1（兼容 Android 5.0）",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_华为 EMUI 3.1（兼容 Android 5.0）",
                                         "id": 27
                                     },
                                     {
                                         "name": "华为 EMUI 4.0（兼容Android 6.0）",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_华为 EMUI 4.0（兼容Android 6.0）",
                                         "id": 28
                                     },
                                     {
                                         "name": "android",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_android",
                                         "id": 29
                                     },
                                     {
                                         "name": "IOS",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_IOS",
                                         "id": 30
                                     },
                                     {
                                         "name": "ios",
                                         "href": "/Api/Goods/goodsList/id/1/attr/69_ios",
                                         "id": 31
                                     }
                                     ]
                        }
*/

}
